/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.musictalk.model.connect;

import com.musictalk.message.model.*;
import com.musictalk.model.business.ManageClientConnThread;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AIDEN
 */
public class ClientConn {

    private Socket s;
    private LoginUser myUser;

    public ClientConn() {
        try {
            s = new Socket("127.0.0.1", 10010);

        } catch (UnknownHostException ex) {
            Logger.getLogger(ClientConn.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClientConn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean sendLoginInfo(Object u){
        boolean result = false;
        try {
            ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(u);

            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            Object temp = ois.readObject();
            Message mes = (Message) temp;
            if (mes.getMsgType()==Message.MSG_SUCCESS) {
                this.myUser = (LoginUser)temp;
                ClientConnThread connThread = new ClientConnThread(s,myUser.getLoginUser());
                connThread.start();
                ManageClientConnThread.addClientThread(myUser.getLoginUser().getUID(), connThread);
                result = true;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientConn.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClientConn.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public LoginUser getMyUser() {
        return myUser;
    }

    
}
